package com.lxh.viewadaptor;

import java.util.ArrayList;

import android.app.Activity;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.lxh.model.ContactInfors;
import com.lxh.model.PhoneNumber;
import com.lxh.updateprefixcode.R;

public class CustomListAdapter extends BaseAdapter {
	private Activity context;
	private ArrayList<ContactInfors> items;
	
	public CustomListAdapter(Activity context){
		this.context = context;
		this.items = new ArrayList<ContactInfors>();
	}
	
	public void setListItems(ArrayList<ContactInfors> list){
		items = list;
	}
	
	@Override
	public int getCount() {
		return this.items.size();
	}

	@Override
	public Object getItem(int position) {
		return this.items.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view =  convertView;
		if (view == null){
			LayoutInflater inflater = LayoutInflater.from(this.context);
			view = inflater.inflate(R.layout.custom_list_item, parent, false);
		}
		
		LinearLayout mainWidget = (LinearLayout) view.findViewById(R.id.main_widget);
		LinearLayout numberWidget = (LinearLayout) view.findViewById(R.id.number_widget);
		ContactInfors contactInfors = this.items.get(position);
	
		ArrayList<PhoneNumber> phoneNumbers = contactInfors.getPhoneNumbers();
		
		if(phoneNumbers.size()>0){
			//------- Add phone number to View --------//
			for(PhoneNumber phoneNumber: phoneNumbers){				
				this.addCustomPhoneNumber(numberWidget, phoneNumber.getPrefixCode(), phoneNumber.getNumber(), false);
				this.addCustomPhoneNumber(numberWidget, phoneNumber.getNewPrefixCode(), phoneNumber.getNewNumber(), true);
			}
			
			//------- Add Submit Button to View ---------//
			this.addContactButton(mainWidget, position);			
		}
		
		
		return view;
	}
	
	private void addContactButton(LinearLayout mainLayout, int tag){
		
		//------- Add Contact Image Button to View ------//
		Button submitBtn = new Button(mainLayout.getContext());
		LayoutParams btnLayoutPrms = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		submitBtn.setLayoutParams(btnLayoutPrms);
		submitBtn.setBackgroundResource(R.drawable.no_image);
		submitBtn.setText("Submit");
		submitBtn.setOnClickListener((OnClickListener) this.context);
		submitBtn.setTag(tag);
		
		mainLayout.addView(submitBtn);
	}

	
	private void addCustomPhoneNumber(LinearLayout mainLayout, String prefixCode, String number, boolean editAble){
		
		//---------- Create and Add sub Layout inside main Layout --------//
		LinearLayout subLayout = new LinearLayout(mainLayout.getContext());
		LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 40);
		subLayout.setOrientation(LinearLayout.HORIZONTAL);
		subLayout.setLayoutParams(layoutParams);
		mainLayout.addView(subLayout);
		
		//---------- Create and Add Field inside SubLayout -----------//
		EditText prefixCodeField = new EditText(subLayout.getContext());
		prefixCodeField.setInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL);
		prefixCodeField.setEms(100);
		LayoutParams prefixLayoutParms = new LayoutParams(80, LayoutParams.MATCH_PARENT);
		prefixCodeField.setLayoutParams(prefixLayoutParms);
		prefixCodeField.setText(prefixCode);
		subLayout.addView(prefixCodeField);
		
		
		EditText numberField = new EditText(subLayout.getContext());
		numberField.setInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL);
		numberField.setEms(100);
		LayoutParams numberLayoutParms = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		numberField.setLayoutParams(numberLayoutParms);
		numberField.setText(number);
		subLayout.addView(numberField);
		
		Log.e("UpdatePrefixCode", prefixCode);
		Log.e("UpdatePrefixCode", number);
	}
}
